package com.longchat.domain;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
@NamedQueries(
			value = { 
					 @NamedQuery(name = "user.findByName", query = "select u from User u where u.name= ?1")
			}
	)
public class User {
	private static final long serialVersionUID = 1L;
	@Id
	private long id;
	@Column(name = "username")
	private String name;
	@Column(name = "password")
	private String password;
	
	
	 /* 
     * @ManyToMany  是多对多关系的一端。 
     * @JoinTable 描述了多对多关系的数据表关系，name属性指定中间表名称。 
     * joinColumns 定义中间表与Teacher 表的外键关系，中间表Teacher_Student的Teacher_ID 列是Teacher 表的主键列对应的外键列。 
     * inverseJoinColumns 属性定义了中间表与另外一端(Student)的外键关系。 
     */ 
	@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)  
	 @JoinTable(name="t_user_role",
	   joinColumns={@JoinColumn(name="user_id",referencedColumnName="id")},    
	   inverseJoinColumns={@JoinColumn(name="role_id",referencedColumnName="id")}    
	 )
	private Set<Role> roles;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}


	
}
